package com.ctrip.demo.leetcode;

import java.util.Arrays;

public class LeetCode747 {

    public static int dominantIndex(int[] nums) {
        int[] clone = nums.clone();
        Arrays.sort(nums);
        if (nums.length == 1) {
            return 0;
        }
        int max = nums[nums.length - 1];
        int index = -1;
        boolean flag = true;
        for (int i = 0; i < clone.length; i++) {
            if (max == clone[i]) {
                index = i;
            }
            if (max != clone[i] && max < clone[i] * 2) {
                flag = false;
            }
        }
        return flag ? index : -1;

    }

    public static int dominantIndex1(int[] nums) {
        int maxIndex = 0;
        int length = nums.length;
        if (length == 0) {
            return 0;
        }

        // 找出最大数字索引
        for (int i = 0; i < length; i++) {
            if (nums[i] > nums[maxIndex]) {
                maxIndex = i;
            }
        }

        for (int i = 0; i < length; i++) {
            if (nums[i] * 2 > nums[maxIndex] && maxIndex != i) {
                return -1;
            }
        }

        return maxIndex;

    }


    public static void main(String[] args) {
        System.out.println(dominantIndex(new int[]{0,0,0,1}));
    }


}
